package com.yckj.ytracker.plugin.springmvc;

import com.yckj.ytracker.api.trace.ITracerClient;
import com.yckj.ytracker.api.factory.TrackerFactory;
import com.yckj.ytracker.api.config.Config;
import com.yckj.ytracker.api.enums.TrackerStatus;
import com.yckj.ytracker.api.enums.TrackerType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.util.Properties;

/**
 * @author liuwei
 * @version 2017/10/14 下午3:03
 * @description springmvc调用链插件
 * @see
 * @since 1.0
 */
public class TrackerPlugin {
    private static final Logger LOGGER = LoggerFactory.getLogger(TrackerPlugin.class);
    static TrackerPlugin plugin = new TrackerPlugin();

    private ITracerClient trackerClient;

    public void init() {
        String configUrl = TrackerPlugin.class.getResource("/").getPath();
        Properties properties = new Properties();
        try {
            String config = configUrl + "dtracker.properties";
            FileInputStream fileInputStream = new FileInputStream(config);
            properties.load(fileInputStream);

        } catch (Exception e) {
            LOGGER.info(configUrl + "dtracker.properties  not found !");
        }
        Config.init(properties);
        trackerClient = TrackerFactory.getTracker(TrackerType.SPRING_MVC);
    }

    int i = 0;

    public void doFilter() {
        try {
            trackerClient.startSpan("doFilter", "springmvc", "sign=1");
            while (i++ < 2) {
                plugin.doFilter();
            }
            trackerClient.offSpan(TrackerStatus.SUCCESS);
        } catch (Exception e) {
            trackerClient.offSpan(TrackerStatus.ERROR, e);
        } finally {
            trackerClient.close();
        }
        return;
    }

    public static void main(String[] args) {

        plugin.init();
        plugin.doFilter();
//        plugin.doFilter();
//        plugin.doFilter();
    }
}
